package com.hellobike.haxing.modules.operate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.common.annotation.AutoLog;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.operate.dto.cms.PageReqDto;
import com.hellobike.haxing.modules.operate.entity.OperateCmsPage;
import com.hellobike.haxing.modules.operate.enums.PageStatusEnum;
import com.hellobike.haxing.modules.operate.service.IOperateCmsPageService;
import com.hellobike.haxing.modules.operate.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 /**
 * @Description: t_operate_cms_page
 * @Author: haxing-boot
 * @Date:   2020-02-09
 * @Version: V1.0
 */
@RestController
@RequestMapping("/operate/operateCmsPage")
@Api(tags="CMS页面管理")
@Slf4j
public class OperateCmsPageController extends BaseController<OperateCmsPage, IOperateCmsPageService> {
	@Autowired
	private IOperateCmsPageService operateCmsPageService;

	/**
	  * 分页列表查询
	  *
	  * @param operateCmsPage
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "t_operate_cms_page-分页列表查询")
	 @GetMapping(value = "/list")
	 public Result<?> queryPageList(OperateCmsPage operateCmsPage,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 QueryWrapper<OperateCmsPage> queryWrapper = QueryGenerator.initQueryWrapper(operateCmsPage, req.getParameterMap());
		 Page<OperateCmsPage> page = new Page<OperateCmsPage>(pageNo, pageSize);
		 IPage<OperateCmsPage> pageList = operateCmsPageService.page(page, queryWrapper);
		 return Result.ok(pageList);
	 }

	 /**
	  * 查询已发布的页面列表
	  *
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "t_operate_cms_page-列表查询")
	 @GetMapping(value = "/list/open")
	 public Result<?> queryList(HttpServletRequest req) {
		 QueryWrapper<OperateCmsPage> queryWrapper = new QueryWrapper();
		 queryWrapper.eq("status", PageStatusEnum.B.getValue());
		 List<OperateCmsPage> list = operateCmsPageService.list(queryWrapper);
		 return Result.ok(list);
	 }
	
	/**
	 *   添加
	 *
	 * @param operateCmsPage
	 * @return
	 */
	@AutoLog(value = "t_operate_cms_page-添加",operateType=2)
	@ApiOperation("CMS页面管理新增接口")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody PageReqDto operateCmsPage) {
		try {
			operateCmsPageService.save(operateCmsPage);
		} catch (Exception e) {
			log.error("t_operate_cms_page-添加",e);
			return Result.error("添加失败！"+e.getMessage());
		}
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param operateCmsPage
	 * @return
	 */
	@AutoLog(value = "t_operate_cms_page-编辑",operateType=3)
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody PageReqDto operateCmsPage) {
		try {
			operateCmsPageService.save(operateCmsPage);
		} catch (Exception e) {
			log.error("t_operate_cms_page-编辑",e);
			return Result.error("编辑失败！"+e.getMessage());
		}
		return Result.ok("编辑成功！");
	}

	 /**
	  *  编辑
	  *
	  * @param operateCmsPage
	  * @return
	  */
	 @AutoLog(value = "t_operate_cms_page-关闭",operateType=3)
	 @PutMapping(value = "/close")
	 public Result<?> close(@RequestBody OperateCmsPage operateCmsPage) {
		 operateCmsPageService.updateById(operateCmsPage);
		 return Result.ok("关闭成功!");
	 }
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "t_operate_cms_page-删除",operateType=4)
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) Long id) {
		operateCmsPageService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "t_operate_cms_page-批量删除",operateType=4)
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.operateCmsPageService.removeByIds(Arrays.asList(ids.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList()));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) Long id) {
		OperateCmsPage operateCmsPage = operateCmsPageService.getById(id);
		if(operateCmsPage==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(operateCmsPage);
	}
	 /**
	  * 通过pageId查询页面信息
	  *
	  * @param id
	  * @return
	  */
	 @GetMapping(value = "/queryByPageId")
	 public Result<?> queryByPageId(@RequestParam(name="id",required=true) Long id) {
		 PageVo pageVo = operateCmsPageService.getByPageId(id);
		 if(pageVo==null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.ok(pageVo);

	 }

    /**
    * 导出excel
    *
    * @param request
    * @param operateCmsPage
    */
    @AutoLog(value = "t_operate_cms_page-导出excel",operateType=6)
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OperateCmsPage operateCmsPage) {
        return super.exportXls(request, operateCmsPage, OperateCmsPage.class, "t_operate_cms_page");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @AutoLog(value = "t_operate_cms_page-导入excel",operateType=5)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OperateCmsPage.class);
    }

}
